<route lang="json5" type="page">
{
  navigationStyle: 'custom',
  style: {
    navigationStyle: 'custom',
    navigationBarTextStyle: 'white',
  },
}
</route>

<template>
  <!-- :style="{ 'padding-top':  }" -->
  <div class="login-container" :style="{ 'padding-top': top + 'px' }">
    <!-- 这里可以添加登录表单等组件 -->
    <view class="login-container-close">
      <wd-icon @click="goBack" class="" name="close-normal" size="100rpx"></wd-icon>
    </view>
    <view class="login-container-content">
      <view class="login-container-content-img">
        <image class="img" :src="indexPic"></image>
      </view>
      <view class="login-container-content-title">买瓷砖，就到格林春天</view>
      <view class="login-container-content-button">
        <wd-button @click="goLogin" custom-class="custom-shadow" size="large" type="warning">
          一键登录
        </wd-button>
      </view>
    </view>
  </div>
</template>

<script setup lang="ts">
// 这里可以编写登录相关的逻辑
import { ref, onMounted } from 'vue'
import index from '@/static/images/index.png'
import { ToastDurationTime } from '@/utils/constant'
import { loginApi } from '@/service/login/index'
import { useUserStore } from '@/store/user'
// 获取pinia 状态
const counter = useUserStore()
const indexPic = ref<string>(index)
const top = ref<number>()

onMounted(() => {
  const { safeAreaInsets } = uni.getSystemInfoSync()
  top.value = safeAreaInsets.top
})

// 登录按钮
const goLogin = async () => {
  uni.login({
    provider: 'weixin', // 使用微信登录
    success: async function (loginRes) {
      console.log(loginRes)
      try {
        const res = await loginApi(loginRes.code)
        // 存储用户信息和token
        counter.setUserInfo({
          id: res.data.id,
          username: res.data.username,
          avatar: res.data.avatar,
          accessToken: res.data.accessToken,
        })
        // 跳转到首页
        uni.switchTab({
          url: '/pages/index/index',
          success: () => {
            uni.showToast({
              title: '登录成功',
              duration: ToastDurationTime,
            })
          },
        })
      } catch (err) {
        uni.showToast({
          title: '登录失败，请稍后重试',
          duration: ToastDurationTime,
        })
      }
    },
    fail: (fail) => {
      uni.showToast({
        title: '登录失败，请稍后重试',
        duration: ToastDurationTime,
      })
    },
  })
}

const goBack = () => {
  uni.navigateBack()
}
</script>

<style scoped lang="scss">
.login-container {
  position: relative;
  height: 100vh;
  background: linear-gradient($uni-home-color 0%, $uni-text-color-inverse 100%);

  &-close {
    // position: absolute;
    // top: 70rpx;
    // left: 30rpx;
  }

  &-content {
    position: absolute;
    top: 40%;
    left: 50%;
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    width: 90%;
    height: 25%;
    transform: translate(-50%, -50%);

    &-img {
      width: 200rpx;
      height: 200rpx;

      .img {
        width: 100%;
        height: 100%;
        border-radius: 10px;
      }
    }

    &-title {
      width: 100%;
      margin-top: 50rpx;
      font-size: 40rpx;
      text-align: center;
    }

    &-button {
      width: 100%;
      margin-top: 100rpx;
      :deep() {
        .custom-shadow {
          width: 100%;
          height: 100rpx;
        }
      }
    }
  }
}
</style>
